import { defineStore } from 'pinia';
import * as actypes from '../types';
import { cacheKey, loadSomething, saveSomething, deleteSomething, clearSomething } from '@/util';

export const usePlayerStore = defineStore('player', {
  state: () => {
    return {
      miniPlayer: true,
      fullPlayer: false,
      /**
       * audio
       */
      audioReady: false,
      audioPlayState: false,
      audioPlayTime: 0,
      audioTotalTime: 360,
      /**
       * song
       */
      currentPlayMode: 0,
      currentPlayIndex: -1,
      currentPlayList: [],
      currentPlayHistory: loadSomething(cacheKey.PLAY_KEY),
      currentSearchHistory: loadSomething(cacheKey.SEARCH_KEY),
      currentFavoriteList: loadSomething(cacheKey.FAVORITE_KEY),
      currentMyPlayList: loadSomething(cacheKey.MY_PLAY_KEY),
      /**
       * lyric
       */
      lyric: null,
      lyricIndex: 0,
      lyricIndexTxt: ''
    };
  },
  getters: {},
  actions: {
    [actypes.SET_MINI_PLAYER](payload) {
      this.miniPlayer = payload;
    },
    [actypes.SET_FULL_PLAYER](payload) {
      this.fullPlayer = payload;
    },
    /**
     * audio
     */
    [actypes.SET_AUDIO_READY](payload) {
      this.audioReady = payload;
    },
    [actypes.SET_AUDIO_PLAY_STATE](payload) {
      this.audioPlayState = payload;
    },
    [actypes.SET_AUDIO_PLAY_TIME](payload) {
      this.audioPlayTime = payload;
    },
    [actypes.SET_AUDIO_TOTAL_TIME](payload) {
      this.audioTotalTime = payload;
    },
    /**
     * song
     */
    [actypes.SET_CURRENT_PLAY_MODE](payload) {
      this.currentPlayMode = payload;
    },
    [actypes.SET_CURRENT_PLAY_INDEX](payload) {
      this.currentPlayIndex = payload;
    },
    [actypes.SET_CURRENT_PLAY_LIST](payload) {
      this.currentPlayList = payload;
    },
    [actypes.ADD_TO_PLAY_HISTORY](payload) {
      this.currentPlayHistory = saveSomething(payload, cacheKey.PLAY_KEY);
    },
    [actypes.ADD_TO_SEARCH_HISTORY](payload) {
      this.currentSearchHistory = saveSomething(payload, cacheKey.SEARCH_KEY, 10);
    },
    [actypes.ADD_TO_FAVORITE_LIST](payload) {
      this.currentFavoriteList = saveSomething(payload, cacheKey.FAVORITE_KEY);
    },
    // 添加到我的歌单
    [actypes.ADD_TO_MY_PLAY_LIST](payload) {
      this.currentMyPlayList = saveSomething(payload, cacheKey.MY_PLAY_KEY);
    },
    // 添加歌曲到下一首播放
    [actypes.ADD_TO_NEXT_PLAY](payload) {
      // 使用原数据的副本
      let playlist = this.currentPlayList.slice();
      let currentIndex = this.currentPlayIndex;
      // 先在下个位置插入歌曲
      currentIndex++;
      playlist.splice(currentIndex, 0, payload);
      // 查找待填加的歌曲是否存在，存在就删除
      let pdIndex = playlist.findIndex(item => item.id == payload.id);
      if (pdIndex > -1) {
        if (pdIndex < currentIndex) {
          playlist.splice(pdIndex, 1);
          currentIndex--;
        } else {
          playlist.splice(pdIndex + 1, 1);
        }
      }
      // 如果播放列表没有歌曲，直接进行播放（player监听了currentIndex）
      if (this.currentPlayList.length == 0) {
        this.SET_CURRENT_PLAY_INDEX(currentIndex);
      }
      this.SET_CURRENT_PLAY_LIST(playlist);
    },
    // 删除搜索记录
    [actypes.DEL_CURRENT_SEARCH_HISTORY](isAll, payload) {
      if (isAll) {
        clearSomething(cacheKey.SEARCH_KEY);
      } else {
        deleteSomething(payload, cacheKey.SEARCH_KEY);
      }
      this.currentSearchHistory = loadSomething(cacheKey.SEARCH_KEY);
    },
    // 删除播放记录
    [actypes.DEL_CURRENT_PLAY_HISTORY](isAll, payload) {
      if (isAll) {
        clearSomething(cacheKey.PLAY_KEY);
      } else {
        deleteSomething(payload, cacheKey.PLAY_KEY);
      }
      this.currentPlayHistory = loadSomething(cacheKey.PLAY_KEY);
    },
    // 删除收藏记录
    [actypes.DEL_CURRENT_FAVORITE_LIST](isAll, payload) {
      if (isAll) {
        clearSomething(cacheKey.FAVORITE_KEY);
      } else {
        deleteSomething(payload, cacheKey.FAVORITE_KEY);
      }
      this.currentFavoriteList = loadSomething(cacheKey.FAVORITE_KEY);
    },
    /**
     * lyric
     */
    [actypes.SET_LYRIC](payload) {
      this.lyric = payload;
    },
    [actypes.SET_LYRIC_INDEX](payload) {
      this.lyricIndex = payload;
    },
    [actypes.SET_LYRIC_INDEX_TXT](payload) {
      this.lyricIndexTxt = payload;
    }
  }
});
